﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using AjaxChatService.Models;

namespace AjaxChatService.YahooMessenger
{
    public class RSSYahooUtils
    {
        private XmlTextReader _reader;
        private XmlDocument _xmlDocument;
        private List<RSSYahooItem> rssItems = new List<RSSYahooItem>();
        public RSSYahooUtils()
        {
            _xmlDocument = new XmlDocument();
        }

        public List<RSSYahooItem> GetRssByTag(String tag)
        {
            // Load the feed in
            _reader = new XmlTextReader("http://rss.news.yahoo.com/rss/" + tag);
            
            // Add the feed to the document
            _xmlDocument.Load(_reader);

            // Get the <feed> element.
            XmlNodeList feeds = _xmlDocument.GetElementsByTagName("item");

            foreach (XmlNode feed in feeds)
            {
                RSSYahooItem rssItem = new RSSYahooItem();

                rssItem.Title = feed.ChildNodes[0].InnerText;
                rssItem.Description = feed.ChildNodes[1].InnerText;
                rssItem.Link = feed.ChildNodes[2].InnerText;

                rssItems.Add(rssItem);
            }

            rssItems.RemoveAt(0);

            return rssItems;
        }
    }
}